package com.turkline.CatchMePolygon;


import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.google.ads.Ad;
import com.google.ads.AdRequest.ErrorCode;

import android.os.Bundle;

public class CatchMePoly extends GLGame 
{
	String screens[] = {"StartScreen" , "LevelSelectScreen" , "GameScreen" };
    private boolean firstCreate = true;

	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

	@Override
	public void onPause()
	{
		Settings.writePrefences(this);
		//Settings.save(getFileIO());
		System.out.println("onPause");
		super.onPause();
	}

	@Override
	public void kill()
	{
		finish();
	}
	
	@Override
	public Screen getStartScreen()
	{
		return new StartScreen(this , 50 , 800 , 480);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config)
	{
		super.onSurfaceCreated(gl, config);
		if(firstCreate)
		{
			Settings.readPreferences(this);
			//Settings.load(getFileIO());
			Assets.load(this);
			firstCreate = false;
		}
		else
		{
			Assets.reload(this);
		}
	}
	
	public boolean dispatchKeyEvent(android.view.KeyEvent event)
	{
		
		String screenName = getCurrentScreen().toString();
		screenName = screenName.substring(screenName.lastIndexOf(".")+1, screenName.indexOf("@"));
		
		 if (event.getKeyCode() == android.view.KeyEvent.KEYCODE_BACK && !screens[0].equalsIgnoreCase(screenName)) 
		 {
			 if(android.view.KeyEvent.ACTION_UP == event.getAction())
			 {
				 if(screenName.equalsIgnoreCase(screens[1]))
				 {
					 setScreen(new StartScreen(this , 50 , 800 , 480) , true);
				 }
				 if(screenName.equalsIgnoreCase(screens[2]))
				 {
					 setScreen(new LevelSelectScreen(this, 100, 800, 480) , true);
				 }
			 }
			 return false;
		 } 
		 else if(event.getKeyCode() == android.view.KeyEvent.KEYCODE_BACK && screens[0].equalsIgnoreCase(screenName)) 
		 {
			 if(android.view.KeyEvent.ACTION_UP == event.getAction())
			 {
				 kill();
			 }
			 
			 return false;
		 }
		 else if(event.getKeyCode() == android.view.KeyEvent.KEYCODE_MENU && "ScreenGame".equalsIgnoreCase(screenName))
		 {
			 if(android.view.KeyEvent.ACTION_UP == event.getAction())
			 {
				 getCurrentScreen().showMenu();
			 }
			 return false;
		 }
		 else 
		 {
			 return super.dispatchKeyEvent(event);
		 }
	}

}